package com.cwl.action;


import java.util.ArrayList;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import com.cwl.domain.Questions;
import com.cwl.service.QuestionService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class QuestionAction extends ActionSupport implements ModelDriven<Questions>{
	private Questions questions= new Questions();
	private QuestionService questionservice;
	private String message;

	String answersum;
	public String getMessage() {
		return message;
	}
	public String getQuestion(){
		Integer qid = new Integer(ServletActionContext.getRequest().getParameter("qid"));
		if(qid<this.questionservice.getArray().length){
			int n = this.questionservice.getCount();
			Integer id = new Integer(this.questionservice.getQid(qid,n));
			Questions newquestions = this.questionservice.findById(id);
			ActionContext.getContext().getValueStack().push(newquestions);
			return SUCCESS;
		}else{
			answersum = ServletActionContext.getRequest().getParameter("answersum");
			String userid = ServletActionContext.getRequest().getParameter("userid");
			int[] array = this.questionservice.getArray();
			String[] answerlist = new String[array.length-1];
			for(int i = 0;i<array.length-1;i++ ){
				if(array[i]!=0){
					String answer = this.questionservice.findById(array[i]).getAnswer();
					answerlist[i] = answer;
				}
			}
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<array.length-1;i++){
				sb.append(array[i]+",");
			}
			ServletActionContext.getContext().getValueStack().set("array", sb.toString());//问题的序号
			ServletActionContext.getContext().getValueStack().set("answersum", answersum);//填写的答案
			ServletActionContext.getContext().getValueStack().set("userid", userid);//用户id
			//answerlist:正确答案的数组
			//answersum:我们答案的字符串
			//array:题号的数组
			String[] answerwe = this.questionservice.toArray(answersum);
			int[] f =this.questionservice.compare(answerlist, answerwe);
			for(int i=0;i<f.length;i++){
				System.out.println(f[i]);
			}
			int[] scorelist = new int[array.length-1];
			for(int i=0;i<array.length-1;i++){
				int score = this.questionservice.findById(array[i]).getScore();
				scorelist[i] = score;
			}
			int mark = this.questionservice.getScoreSum(scorelist, f);
			//把分数显示出来，再保存
			ServletActionContext.getContext().getValueStack().set("mark", mark);//分数
			//错误的题目序号
			ArrayList<Integer> errorlist = this.questionservice.errorTitle(f, array);
			ServletActionContext.getContext().getValueStack().set("errorlist", errorlist);//分数
			ServletActionContext.getContext().getValueStack().set("f", f);//分数
			return "answer";
		}
	}
	public String select(){
		Questions questions = this.questionservice.findById(this.getModel().getQid());
		BeanUtils.copyProperties(questions, this.getModel());
		String question = questions.getQuestion();
		ServletActionContext.getContext().getValueStack().set("question", question);
		String answera = questions.getAnswerA();
		ServletActionContext.getContext().getValueStack().set("answera", answera);
		String answere = questions.getAnswerE();
		ServletActionContext.getContext().getValueStack().set("answere", answere);
		return "select";
	}
	public String selectAll(){
		int[] array = this.questionservice.getArray();
		ArrayList<Questions> questionslist=this.questionservice.findAll(array);
		System.out.println(questionslist.get(0).getQuestion());;
//		ServletActionContext.getContext().getValueStack().set("questionslist", questionslist);
		ServletActionContext.getRequest().setAttribute("questionslist", questionslist);
		return "selectall";	
	}
	public String add(){
		Questions newquestions = new Questions();
		System.out.println(this.getModel().getQuestion());
		BeanUtils.copyProperties(this.getModel(), newquestions);
		this.questionservice.save(newquestions);
		return "add";
	}
	public Questions getModel() {
		// TODO Auto-generated method stub
		return questions;
	}
	public QuestionService getQuestionservice() {
		return questionservice;
	}
	public void setQuestionservice(QuestionService questionservice) {
		this.questionservice = questionservice;
	}

}

